package com.kevin.enterprise.controller;

import com.kevin.enterprise.entity.Student;
import com.kevin.enterprise.service.StudentService;
import com.kevin.enterprise.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.util.List;


@WebServlet(name = "StudentController")
public class StudentController extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri=request.getRequestURI();
        System.out.println(uri);
        uri=uri.substring(31);//获取从第31个字符串开始
        //uri.substring(uri.indexOf("/"),uri.lastIndexOf(""));
        System.out.println(uri);

       /* if("studentList".equals(uri)){
            studentList(request,response);
        }else  if("addStudent".equals(uri)){
            addStudent(request,response);

        }else  if("deleteStudent".equals(uri)){
            deleteStudent(request,response);
        }else  if("updateStudent".equals(uri)){
            updateStudent(request,response);
        }else  if("modifyStudent".equals(uri)){
            modifyStudent(request,response);
        }*/

    }

    private void deleteStudent(HttpServletRequest request, HttpServletResponse response) {
        try {
            String sno=request.getParameter("sno");
            System.out.println("sno:"+sno);
            studentService.deleteByOne(sno);
            request.getRequestDispatcher("studentList").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void updateStudent(HttpServletRequest request, HttpServletResponse response) {
        String sno = request.getParameter("sno");

        try {
            Student student = studentService.updateByOne(sno);
            request.setAttribute("student",student);
            request.getRequestDispatcher("updateStudent.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void modifyStudent(HttpServletRequest request, HttpServletResponse response) {

        try {
            Student student =new Student();
            //学号	姓名	性别	登录密码	出生日期	特长	总分
            student.setSno(request.getParameter("sno"));
            student.setSname( new String (request.getParameter("sname").getBytes("ISO-8859-1"),"UTF-8"));
            student.setPassword(request.getParameter("password"));
            student.setSsex(new String (request.getParameter("ssex").getBytes("ISO-8859-1"),"UTF-8"));
            student.setSbirthday(Date.valueOf(request.getParameter("sbirthday")));
            student.setSpeciality(new String (request.getParameter("speciality").getBytes("ISO-8859-1"),"UTF-8"));
            student.setTc(Integer.parseInt(request.getParameter("tc")));
            studentService.modifyStudent(student);
            request.getRequestDispatcher("studentList").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addStudent(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException
    {
        try {
            String sno=request.getParameter("sno");
            String sname= new String (request.getParameter("sname").getBytes("ISO-8859-1"),"UTF-8");
            String password=request.getParameter("password");
            String ssex=new String (request.getParameter("ssex").getBytes("ISO-8859-1"),"UTF-8");
            System.out.println(sno+sname+password+ssex);
            Student student =new Student();

            student.setSno(sno);
            student.setSname(sname);
            student.setPassword(password);
            student.setSsex(ssex);

            if(studentService.AddByOne(student)){
                request.getRequestDispatcher("studentList").forward(request,response);
            }
            request.getRequestDispatcher("studentList").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void studentList(HttpServletRequest request, HttpServletResponse response) {
        List<Student> studentList =studentService.studentList();

        try {
            request.setAttribute("studentList",studentList);
            request.getRequestDispatcher("studentList.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
